//
// [Author] lg (https://github.com/lemos1235)
// [Date] 2024/4/12
//
import 'dart:math' as math;

import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:queyue/widgets/draggable_scrollable_sheet.dart';
import 'package:queyue/widgets/picture_header.dart';

class MusicDetailPage extends StatefulWidget {
  const MusicDetailPage({super.key});

  @override
  State<MusicDetailPage> createState() => _MusicDetailPageState();
}

class _MusicDetailPageState extends State<MusicDetailPage> {
  List<Color> colorList = [Colors.green, Colors.red, Colors.white];

  late EasyRefreshController _controller;

  @override
  void initState() {
    super.initState();
    _controller = EasyRefreshController(
      controlFinishRefresh: true,
      controlFinishLoad: true,
    );
  }

  Future<void> handleRefresh() async {
    // await Future.delayed(Duration(seconds: 1));
    // setState(() {
    //   colorList = [Colors.green, Colors.red];
    // });
    _controller.finishRefresh();
    _controller.resetFooter();
  }

  void handleLoad() async {
    await Future.delayed(Duration(seconds: 1));
    setState(() {
      final colors = [Colors.red, Colors.yellow, Colors.blue, Colors.green];
      final randomColor = colors[math.Random().nextInt(3)];
      colorList = [...colorList, randomColor];
    });
    _controller.finishLoad(IndicatorResult.success);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Refresh Demo")),
      body: Stack(
        children: [
          EasyRefresh.builder(
            onRefresh: handleRefresh,
            // onLoad: handleLoad,
            header: PictureHeader(
              position: IndicatorPosition.behind,
              background: Image.network(
                "https://r11.realme.net/CN/thread/1555770224727732224.jpg",
                fit: BoxFit.fitWidth,
                alignment: Alignment.topCenter,
              ),
            ),
            childBuilder: (context, physics) {
              return SuperDraggableScrollableSheet(
                  minChildSize: 0.8,
                  initialChildSize: 0.8,
                  maxChildSize: 1.0,
                  builder: (context, scrollController) {
                    return Container(
                      color: Colors.white60,
                      child: Column(
                        children: [
                          Container(
                            padding: EdgeInsets.symmetric(vertical: 12, horizontal: 10),
                            width: double.infinity,
                            height: 100,
                            color: Colors.grey,
                            child: Text("标题"),
                          ),
                          Expanded(
                            child: ListView(
                              physics: physics,
                              controller: scrollController,
                              children: [
                                for (var color in colorList)
                                  Container(
                                    height: 100,
                                    color: color,
                                  ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    );
                  });
            },
          ),
        ],
      ),
    );
  }
}
